package com.lovo.project.servlet.pet;

import com.lovo.project.exception.OurException;
import com.lovo.project.model.PetDO;
import com.lovo.project.service.PetService;
import com.lovo.project.service.impl.PetServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;

@WebServlet(name = "AddPetServlet",value = "/pet/addPet")
public class AddPetServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String name = request.getParameter("petName");
            String _class = request.getParameter("petClass");
            String type = request.getParameter("petType");
            String color = request.getParameter("petColor");
//            Timestamp birthday = Timestamp.valueOf(request.getParameter("petBirthday"));
            System.out.println(request.getParameter("petBirthday"));
            String character = request.getParameter("petCharacter");
            Integer petOwnerId = Integer.parseInt(request.getParameter("petOwnerId"));
            PetDO pet = new PetDO();
            pet.setPetOwnerId(petOwnerId);
            pet.setPetName(name);
            pet.setPetClass(_class);
            pet.setPetType(type);
            pet.setPetColor(color);
//            pet.setPetBirthday(birthday);
            pet.setPetCharacter(character);
            PetService service = new PetServiceImpl();
            service.insertPet(pet);
            response.getWriter().print(ResultVO.success().toJson());
        }catch (OurException o){
            response.getWriter().print(ResultVO.fail(o.getCodeEnum()));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
